package com.example.demo.controller;

import com.example.demo.common.constants.SessionContents;
import com.example.demo.common.session.SessionComponent;
import com.example.demo.model.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

/**
 * @author admin
 * @version 1.0.0
 * @time 2021/5/30 21:31
 * @desc TODO
 */
@RestController
@RequestMapping("/login")
public class LoginController {


    @Autowired
    private SessionComponent sessionComponent;

    /**
     * 登入
     * @param request
     * @param username
     * @return
     */
    @GetMapping("/doLogin/{username}")
    public HttpResult doLogin(HttpServletRequest request, @PathVariable String username) {
        String sessionId = request.getSession().getId();

        sessionComponent.doLogin(sessionId, username);

        return HttpResult.success(username);
    }

    /**
     * 登出
     * @param request
     * @return
     */
    @GetMapping("/doLogout")
    public HttpResult doLogout(HttpServletRequest request) {
        String sessionId = request.getSession().getId();

        sessionComponent.doLogout(sessionId);

        return HttpResult.success();
    }

}
